package ar.edu.unq.wannaplay.services;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.joda.time.DateTime;
import org.joda.time.Interval;
import org.springframework.transaction.annotation.Transactional;

import ar.edu.unq.wannaplay.client.Client;
import ar.edu.unq.wannaplay.dtos.EmployeeDTO;
import ar.edu.unq.wannaplay.dtos.RegisterDTO;
import ar.edu.unq.wannaplay.dtos.SkillDTO;
import ar.edu.unq.wannaplay.employee.Experience;
import ar.edu.unq.wannaplay.employee.PersonalData;
import ar.edu.unq.wannaplay.employee.SocialWork;
import ar.edu.unq.wannaplay.exceptions.NegativeNumberException;
import ar.edu.unq.wannaplay.helpers.CareerPlanOrganizer;
import ar.edu.unq.wannaplay.helpers.LicenceSpitter;
import ar.edu.unq.wannaplay.ocupation.CareerPlan;
import ar.edu.unq.wannaplay.ocupation.Ocupation;
import ar.edu.unq.wannaplay.ocupation.Percentages;
import ar.edu.unq.wannaplay.users.Role;

public class DataGeneratorService implements Serializable {
    private static final long serialVersionUID = 1L;

    private ProjectService projectService;

    private CareerPlanService careerPlanService;

    private EmployeeService employeeService;

    private LicenceService licenceService;

    private UserService userService;

    private ClientService clientService;

    private CareerPlan careerPlan1;

    private boolean justPersisted = false;

    @Transactional
    public void initializeData() {
        if (!justPersisted) {
            this.createAdmin();
            this.createUser();
            this.createLicenceSpitter();
            this.createCareerPlans();
            this.createEmployees();
            this.createProjects();
            justPersisted = true;
        }
    }

    private void createAdmin() {
        try {
            userService.register("admin", "admin", Role.ADMIN);
        } catch (Exception e) {
            e.getMessage();
        }
    }

    private void createUser() {
        try {
            userService.register("user1", "user1", Role.USER);
        } catch (Exception e) {
            e.getMessage();
        }
    }

    private void createProjects() {
        Client client1 = new Client("Cristian", "clopez@gmail.com", "Calle Falsa 123", "Quilmes");
        DateTime oneOfApril = new DateTime(2011, 10, 6, 0, 0, 0, 0);
        DateTime fifteenOfApril = new DateTime(2011, 10, 29, 0, 0, 0, 0);
        Interval interval1 = new Interval(oneOfApril, fifteenOfApril);

        List<SkillDTO> requirements1 = new ArrayList<SkillDTO>();
        SkillDTO skillDTO1a = new SkillDTO();
        skillDTO1a.setExperience(Experience.BASIC);
        skillDTO1a.setKnowledge("Java");
        requirements1.add(skillDTO1a);

        SkillDTO skillDTO2a = new SkillDTO();
        skillDTO2a.setExperience(Experience.BASIC);
        skillDTO2a.setKnowledge("Wicket");
        requirements1.add(skillDTO2a);

        List<EmployeeDTO> employees = new ArrayList<EmployeeDTO>();

        this.createAndPersistProjectDTO("project1", client1, interval1, requirements1, employees, 500);
    }

    private void createEmployees() {
        EmployeeDTO e1 = new EmployeeDTO();
        PersonalData data1 = new PersonalData("Cristian", "Suarez", "35156460", "Pringles 661", "Bernal", "4252-7888",
                "cris.eze.sua@gmail.com", "UNQ");
        Ocupation ocupation1 = new Ocupation(careerPlan1, "Junior", 50);

        List<SkillDTO> skills1 = new ArrayList<SkillDTO>();
        SkillDTO skillDTO1e = new SkillDTO();
        skillDTO1e.setKnowledge("java");
        skillDTO1e.setExperience(Experience.MEDIUM);
        skills1.add(skillDTO1e);
        SkillDTO skillDTO2e = new SkillDTO();
        skillDTO2e.setKnowledge("Wicket");
        skillDTO2e.setExperience(Experience.BASIC);
        skills1.add(skillDTO2e);

        // DateTime startInterval1e1 = new DateTime(2011, 12, 15, 0, 0, 0, 0);
        // DateTime endInterval1e1 = new DateTime(2011, 12, 30, 0, 0, 0, 0);
        // Interval licenceInterval1e1 = new Interval(startInterval1e1,
        // endInterval1e1);
        //
        // e1.setRegister(new RegisterDTO());
        // List<LicenceDTO> licences1 = new ArrayList<LicenceDTO>();
        // licences1.add(new LicenceDTO(new Licence("Holiday",
        // licenceInterval1e1)));

        this.createAndPersistEmployeeDTO(e1, new RegisterDTO(), data1, new SocialWork("ABC"), "art1", ocupation1,
                skills1);
    }

    private void createCareerPlans() {
        CareerPlanOrganizer careerPlanOrganizer = new CareerPlanOrganizer();

        String junior = "Junior";
        String semiSenior = "Semi Senior";
        String senior = "Senior";
        String lider = "Lider";
        Percentages juniorRangeList = careerPlanOrganizer.buildRange(0, 50, 100);
        careerPlanOrganizer.addNewRank(junior, juniorRangeList);
        Percentages semiSeniorRangeList = careerPlanOrganizer.buildRange(0, 50, 100);
        careerPlanOrganizer.addNewRank(semiSenior, semiSeniorRangeList);
        Percentages seniorRangeList = careerPlanOrganizer.buildRange(0, 33, 66, 100);
        careerPlanOrganizer.addNewRank(senior, seniorRangeList);
        Percentages liderRangeList = careerPlanOrganizer.buildRange(0, 25, 50, 75, 100);
        careerPlanOrganizer.addNewRank(lider, liderRangeList);

        String tecnico = "Tecnico";
        careerPlan1 = careerPlanOrganizer.createCarrerPlan(tecnico);
        careerPlanOrganizer.addSalaries(tecnico, junior, 1500, 2500);
        careerPlanOrganizer.addSalaries(tecnico, semiSenior, 3000, 4000);
        careerPlanOrganizer.addSalaries(tecnico, senior, 5000, 6000);
        careerPlanOrganizer.addSalaries(tecnico, lider, 7000, 8000);

        String tester = "Tester";
        careerPlanOrganizer.createCarrerPlan(tester);
        careerPlanOrganizer.addSalaries(tester, junior, 1500, 2500);
        careerPlanOrganizer.addSalaries(tester, semiSenior, 3000, 4000);
        careerPlanOrganizer.addSalaries(tester, senior, 5000, 6000);
        careerPlanOrganizer.addSalaries(tester, lider, 7000, 8000);

        String funcional = "Funcional";
        careerPlanOrganizer.createCarrerPlan(funcional);
        careerPlanOrganizer.addSalaries(funcional, junior, 1500, 2500);
        careerPlanOrganizer.addSalaries(funcional, semiSenior, 3000, 4000);
        careerPlanOrganizer.addSalaries(funcional, senior, 5000, 6000);
        careerPlanOrganizer.addSalaries(funcional, lider, 7000, 8000);

        String diseniador = "Diseñador";
        careerPlanOrganizer.createCarrerPlan(diseniador);
        careerPlanOrganizer.addSalaries(diseniador, junior, 1500, 2500);
        careerPlanOrganizer.addSalaries(diseniador, semiSenior, 3000, 4000);
        careerPlanOrganizer.addSalaries(diseniador, senior, 5000, 6000);
        careerPlanOrganizer.addSalaries(diseniador, lider, 7000, 8000);

        careerPlanService.saveCareerPlanOrganizer(careerPlanOrganizer);
    }

    private void createLicenceSpitter() {
        LicenceSpitter licenceSpitter = new LicenceSpitter();

        try {
            licenceSpitter.addLicence("Holiday", 30, 1);
            licenceSpitter.addLicence("Sickness", 40, 2);
        } catch (NegativeNumberException e) {
            e.getStackTrace();
        }

        licenceService.saveLicenceSpitter(licenceSpitter);
    }

    private void createAndPersistEmployeeDTO(final EmployeeDTO empDTO, final RegisterDTO registerDTO,
            final PersonalData data, final SocialWork socialWork, final String art, final Ocupation ocupation,
            final List<SkillDTO> skills) {
        empDTO.setRegister(registerDTO);
        empDTO.setData(data);
        empDTO.setSocialPlan(socialWork);
        empDTO.setArt(art);
        empDTO.setOcupation(ocupation);
        empDTO.setSkills(skills);

        employeeService.saveEmployee(empDTO);
    }

    private void createAndPersistProjectDTO(final String name, final Client client, final Interval interval,
            final List<SkillDTO> requirements, final List<EmployeeDTO> employees, final int manHour) {

        try {
            projectService.saveProject(manHour, name, client, requirements, interval, employees);
        } catch (NegativeNumberException e) {
            e.getStackTrace();
        }
    }

    public ProjectService getProjectService() {
        return projectService;
    }

    public void setProjectService(final ProjectService projectService) {
        this.projectService = projectService;
    }

    public CareerPlanService getCareerPlanService() {
        return careerPlanService;
    }

    public void setCareerPlanService(final CareerPlanService careerPlanService) {
        this.careerPlanService = careerPlanService;
    }

    public EmployeeService getEmployeeService() {
        return employeeService;
    }

    public void setEmployeeService(final EmployeeService employeeService) {
        this.employeeService = employeeService;
    }

    public LicenceService getLicenceService() {
        return licenceService;
    }

    public void setLicenceService(final LicenceService licenceService) {
        this.licenceService = licenceService;
    }

    public CareerPlan getCareerPlan1() {
        return careerPlan1;
    }

    public void setCareerPlan1(final CareerPlan careerPlan1) {
        this.careerPlan1 = careerPlan1;
    }

    public UserService getUserService() {
        return userService;
    }

    public void setUserService(final UserService userservice) {
        userService = userservice;
    }

    public boolean isJustPersisted() {
        return justPersisted;
    }

    public void setJustPersisted(final boolean justPersisted) {
        this.justPersisted = justPersisted;
    }

    public ClientService getClientService() {
        return clientService;
    }

    public void setClientService(final ClientService clientService) {
        this.clientService = clientService;
    }

}
